/**
 * 329. 矩阵中的最长递增路径
 * https://leetcode-cn.com/problems/longest-increasing-path-in-a-matrix/
 */
public class Solutions_329 {
    public static void main(String[] args) {
//        int[][] matrix = {{9, 9, 4},
//                          {6, 6, 8},
//                          {2, 1, 1}};  // output: 4

        int[][] matrix = {{3, 4, 5},
                          {3, 2, 6},
                          {2, 2, 1}} ;  // output: 4

        int result = longestIncreasingPath(matrix);
        System.out.println(result);
    }

    /**
     * 解法二：记忆化递归（20ms）
     */
    private static int row = 0, col = 0;
    // memory[2][1] = 4，表示 matrix[2][1] 中的元素已经递归查找过，
    // 且从 matrix[2][1] 为起点开始搜索，得到的最大递增路径长度，结果为 4
    private static int[][] memory = null;
    public static int longestIncreasingPath(int[][] matrix) {
        int res = 0;
        if (matrix == null || matrix.length < 1 || matrix[0].length < 1) {
            return res;
        }
        row = matrix.length;
        col = matrix[0].length;
        memory = new int[row][col];

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                int curNum = matrix[i][j];
                // 上、下、左、右四个方向搜索
                int upRes = dfs2(matrix, i - 1, j, 0, curNum);
                int downRes = dfs2(matrix, i + 1, j, 0, curNum);
                int leftRes = dfs2(matrix, i, j - 1, 0, curNum);
                int rightRes = dfs2(matrix, i, j + 1, 0, curNum);
                res = Math.max(res, getMax(upRes, downRes, leftRes, rightRes) + 1);
            }
        }
        return res;
    }

    public static int dfs2(int[][] matrix, int i, int j, int len, int lastNum) {
        if (i < 0 || j < 0 || i >= row || j >= col || matrix[i][j] <= lastNum) {
            // 结束条件：索引越界，或者小于路径中的前一个数，不构成递增
            return len;
        }
        if (memory[i][j] > 0) {
            // 以 [i, j] 为起点的最长递增路径长度已经计算出，不需要重复递归计算
            return len + memory[i][j];
        }
        int curNum = matrix[i][j];
        // 上、下、左、右四个方向搜索
        int upRes = dfs2(matrix, i - 1, j, len, curNum);
        int downRes = dfs2(matrix, i + 1, j, len, curNum);
        int leftRes = dfs2(matrix, i, j - 1, len, curNum);
        int rightRes = dfs2(matrix, i, j + 1, len, curNum);
        int curRes = getMax(upRes, downRes, leftRes, rightRes) + 1;
        memory[i][j] = curRes;
        return curRes;
    }

    /**
     * 解法一：递归（超时）
     */
//    private static int row = 0, col = 0;
    public static int longestIncreasingPath2(int[][] matrix) {
        int res = 0;
        if (matrix == null || matrix.length < 1 || matrix[0].length < 1) {
            return res;
        }
        row = matrix.length;
        col = matrix[0].length;
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                // 以 [i, j] 为原点，向四个方向搜索
                int upRes = dfs(matrix, i - 1, j, 1, matrix[i][j]);
                int downRes = dfs(matrix, i + 1, j, 1, matrix[i][j]);
                int leftRes = dfs(matrix, i, j - 1, 1, matrix[i][j]);
                int rightRes = dfs(matrix, i, j + 1, 1, matrix[i][j]);
                int cur = getMax(upRes, downRes, leftRes, rightRes);
                res = Math.max(res, cur);
            }
        }
        return res;
    }

    public static int dfs(int[][] matrix, int i, int j, int len, int num) {
        if (i < 0 || j < 0 || i >= row || j >= col || matrix[i][j] <= num) {
            // 结束条件：索引越界，或者小于路径中的前一个数，不构成递增
            return len;
        }
        len ++;
        int upRes = dfs(matrix, i - 1, j, len, matrix[i][j]);
        int downRes = dfs(matrix, i + 1, j, len, matrix[i][j]);
        int leftRes = dfs(matrix, i, j - 1, len, matrix[i][j]);
        int rightRes = dfs(matrix, i, j + 1, len, matrix[i][j]);
        int res = getMax(upRes, downRes, leftRes, rightRes);
        return res;
    }

    /**
     * 获取数组 a 中的最大值
     */
    public static int getMax(int... a) {
        int res = a[0];
        for (int i : a) {
            res = Math.max(res, i);
        }
        return res;
    }
}
